package jgl.vajra.kingkong.widget.retrofithelper.interceptor;

import android.util.Log;

import com.orhanobut.logger.Logger;
import jgl.vajra.kingkong.widget.utils.CommonUtils;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/3/15
 * @Describe
 */

public class NetworkInterceptor implements Interceptor {
    public static final Charset UTF8 = Charset.forName("UTF-8");

    public enum Level {
        /**
         * No logs.
         */
        NONE,
        /**
         * Logs request and response lines.
         * <p>
         * <p>Example:
         * <pre>{@code
         * --> POST /greeting http/1.1 (3-byte body)
         *
         * <-- 200 OK (22ms, 6-byte body)
         * }</pre>
         */
        BASIC,
        /**
         * Logs request and response lines and their respective headers.
         * <p>
         * <p>Example:
         * <pre>{@code
         * --> POST /greeting http/1.1
         * Host: example.com
         * Content-Type: plain/text
         * Content-Length: 3
         * --> END POST
         *
         * <-- 200 OK (22ms)
         * Content-Type: plain/text
         * Content-Length: 6
         * <-- END HTTP
         * }</pre>
         */
        HEADERS,
        /**
         * Logs request and response lines and their respective headers and bodies (if present).
         * <p>
         * <p>Example:
         * <pre>{@code
         * --> POST /greeting http/1.1
         * Host: example.com
         * Content-Type: plain/text
         * Content-Length: 3
         *
         * Hi?
         * --> END POST
         *
         * <-- 200 OK (22ms)
         * Content-Type: plain/text
         * Content-Length: 6
         *
         * Hello!
         * <-- END HTTP
         * }</pre>
         */
        BODY
    }


    public NetworkInterceptor() {

    }

    private volatile Level level = Level.NONE;

    /**
     * Change the level at which this interceptor logs.
     */
    public NetworkInterceptor setLevel(Level level) {
        if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
        this.level = level;
        return this;
    }

    public Level getLevel() {
        return level;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        StringBuffer bfLog = new StringBuffer();
        bfLog.append("接口访问开始=======================================接口访问开始========================================");
        Request request = chain.request();
        Response response;
        try {
            response = chain.proceed(request);
            CommonUtils.printNetLog(chain, request, response, bfLog, level);
        } catch (Exception e) {
            //response = chain.proceed(request);
            bfLog.append("<-- HTTP FAILED: \n" + e.getMessage() + "\n");
            //  HttpLogManager.getInstance().recordLog(bfLog.toString());
            //  L.d(bfLog.toString());
            Log.d("----", bfLog.toString());
            //  ErrorManager.handleError(new DefaultErrorBundle((Exception) e));
            throw e;
        }

        Logger.d(bfLog.toString());
        return response;
    }


}
